package com.demandbreakdown.xiyun.config;

import cn.dev33.satoken.stp.StpInterface;
import com.demandbreakdown.xiyun.model.entity.User;
import com.demandbreakdown.xiyun.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * SaToken权限验证接口实现类
 *
 * @author poxz
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class StpInterfaceImpl implements StpInterface {

    private final UserService userService;

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<String> roleList = new ArrayList<>();
        try {
            Long userId = Long.valueOf(loginId.toString());
            User user = userService.getById(userId);

            if (user != null && user.getUserRole() != null) {
                roleList.add(user.getUserRole());
            }
        } catch (Exception e) {
            log.error("获取用户角色失败: loginId={}, error={}", loginId, e.getMessage());
        }
        return roleList;
    }

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        return new ArrayList<>();
    }
}
